<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable>
                <el-button slot="append" icon="el-icon-search" @click="getGoodsList" ></el-button>
            </el-input>
            <el-button type="primary" @click="addShop">添加商品</el-button>

            <!--         表格区域-->
            <el-table :data="goodslist" border>
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="goods_name"  label="商品名称" ></el-table-column>
                <el-table-column  prop="goods_price" label="商品价格($)" width="100px"></el-table-column>
                <el-table-column  prop="goods_weight" label="商品重量" width="80px"></el-table-column>
                <el-table-column  prop="add_time" label="创建时间" width="200px">
                    <template slot-scope="scope">
                        {{scope.row.add_time | dataFormat}}
                    </template>
                </el-table-column>
                <el-table-column  label="操作" width="200px">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini">编辑</el-button>
                        <el-button type="danger" size="mini" @click="removeClick(scope.row.goods_id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

<!--            分页区域-->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum"
                    :page-sizes="[1, 2, 3, 10]"
                    :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total" background>
            </el-pagination>
        </el-card>


    </div>
</template>

<script>
  export default {
    name: "goodsList",
    data() {
      return {
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 10,
        },
        goodslist: [],
        total: 0,
      }
    },
    methods: {
      //根据页码和搜索条件获取商品列表的所有数据
      async getGoodsList() {
        const {data: res} = await this.$http.get('goods', {
          params:
          this.queryInfo
        })
        if (res.meta.status !== 200) return this.$message.error('获取商品列表失败')
        this.goodslist = res.data.goods
        this.total = res.data.total
      },
      //页码改变时触发
      handleSizeChange(newSize){
         this.queryInfo.pagesize = newSize
        this.getGoodsList()
      },
      //页数改变时触发
      handleCurrentChange(newPage){
          this.queryInfo.pagenum = newPage
        this.getGoodsList()
      },

      //删除商品按钮的点击
      async removeClick(id){
        const{data:res} = await this.$http.delete(`goods/${id}`)
        if (res.meta.status !==200) return this.$message.error('商品删除失败')
        this.$message.success('商品删除成功')
        this.getGoodsList()
      },
      //添加商品
      addShop(){
        this.$router.push('/goods/add')
      }
    },
    created() {
      this.getGoodsList()
    }
  }
</script>

<style scoped>
    .el-input {
        width: 300px;
        margin-right: 10px;
    }
</style>